define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'report/finance/index' + location.search,
                    add_url: 'report/finance/add',
                    // edit_url: 'report/finance/edit',
                    // del_url: 'report/finance/del',
                    multi_url: 'report/finance/multi',
                    import_url: 'report/finance/import',
                    table: 'finance',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);

                $("input[name='unit_id']", form).addClass("selectpage").data("source", "basic/unit/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");

                $("input[name='stash_id']", form).addClass("selectpage").data("source", "basic/stash/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");

                $("input[name='staff_id']", form).addClass("selectpage").data("source", "basic/staff/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");

                $("input[name='assets_id']", form).addClass("selectpage").data("source", "basic/assets/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");

                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate: false},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        {
                            field: 'date',
                            title: __('Date'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {field: 'staff_id', title: __('Staff_id')},
                        {field: 'unit_id', title: __('Unit_id')},
                        {field: 'assets_id', title: __('Assets_id')},
                     //   {field: 'be', title: __('Be')},
                        {field: 'price', title: __('Price'), operate: 'BETWEEN'},
                        {
                            field: 'status', title: __('类型'),
                            searchList: {
                                "100": __('Status 100'),
                                "200": __('Status 200'),
                                "300": __('Status 300'),
                                "400": __('Status 400'),
                                "500": __('Status 500'),
                                "600": __('收款单'),
                                "700": __('付款单'),
                                "800": __('项目收入单'),
                                "900": __('项目支出单'),
                                "1000": __('转账单'),
                                "1300": __('调拨单'),
                                "1400": __('其他入库单'),
                                "1500": __('其他出库单'),
                            }, formatter: Table.api.formatter.status
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        // {
                        //     field: 'updatetime',
                        //     title: __('Updatetime'),
                        //     operate: 'RANGE',
                        //     addclass: 'datetimerange',
                        //     autocomplete: false,
                        //     formatter: Table.api.formatter.datetime
                        // },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons:[
                                {
                                    name: 'detail',
                                    text: __('详情'),
                                    title: __('详情'),
                                    extend:'data-area=["95%","95%"]',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'report/finance/detail/order_no/{order_no}/status/{status}',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'report/finance/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'report/finance/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'report/finance/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
